package com.yonyou.cyxdms.sysAuth.service.service.manage.asc;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yonyou.cyx.framework.util.bean.ApplicationContextHelper;
import com.yonyou.cyx.function.exception.ServiceBizException;

import com.yonyou.cyx.function.utils.common.StringUtils;
import com.yonyou.cyxdms.framework.bean.dto.user_info.LoginInfoDto;

import com.yonyou.cyxdms.sysAuth.service.constants.ManageDictCodeConstants;
import com.yonyou.cyxdms.sysAuth.service.entity.AscInfoPO;
import com.yonyou.cyxdms.sysAuth.service.entity.CompanyPO;
import com.yonyou.cyxdms.sysAuth.service.entity.OrgPO;
import com.yonyou.cyxdms.sysAuth.service.entity.TrOrgPO;
import com.yonyou.cyxdms.sysAuth.service.mapper.AscInfoMapper;
import com.yonyou.cyxdms.sysAuth.service.mapper.CompanyMapper;
import com.yonyou.cyxdms.sysAuth.service.mapper.OrgMapper;
import com.yonyou.cyxdms.sysAuth.service.mapper.TrOrgMapper;
import com.yonyou.cyxdms.sysAuth.service.service.org.OrgService;
import com.yonyou.cyxdms.sysmanage.client.constants.BaseDataDictConstants;
import com.yonyou.cyxdms.sysmanage.client.domains.dto.AscInfoDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class AscInfoServiceImpl implements AscInfoService {

    @Autowired
    private AscInfoMapper ascInfoMapper;
    @Autowired
    CompanyMapper companyMapper;
    @Autowired
    private AscInfoService ascInfoService;
    @Autowired
    private OrgMapper orgMapper;
    @Autowired
    private TrOrgMapper trOrgMapper;

    @Autowired
    OrgService orgService;

    /**
     * 保存服务站信息
     *
     * @auther: 李贺
     * @since: 2019/9/10
     */
    @Override
    public void addAscInfo(AscInfoPO ascInfoPO) {
        ascInfoMapper.insert(ascInfoPO);
    }

    /**
     * 更新服务站信息
     *
     * @auther: 李贺
     * @since: 2019/9/16
     */
    @Override
    public void updateAscById(AscInfoPO ascInfoPO) {
        ascInfoMapper.updateById(ascInfoPO);
    }

    /**
     * 查询服务站信息
     *
     * @auther: 李贺
     * @since: 2019/9/16
     */
    @Override
    public AscInfoPO selectAscById(Integer ascId) {
        return ascInfoMapper.selectById(ascId);
    }

    /**
     * 售后信息查询
     *
     * @return
     */
    @Override
    @Transactional(readOnly = true)
    public IPage<Map<String, Object>> selectPageAfterSaleInfo(Page page, Map<String,
            Object> queryCondition) {
        // 获取总条数
        page.setRecords(ascInfoMapper.selectPageAfterSaleInfo(page, queryCondition));
        return page;
    }

    @Override
    public Integer insertAfterSaleInfo(AscInfoPO ascInfoPO) {
        LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        LambdaQueryWrapper<AscInfoPO> queryWrapper = new QueryWrapper<AscInfoPO>().lambda();
        queryWrapper.eq(AscInfoPO::getAppId, loginInfoDto.getAppId());
        queryWrapper.eq(AscInfoPO::getCompanyId, ascInfoPO.getCompanyId());
        AscInfoPO ascInfoPO1 = ascInfoMapper.selectOne(queryWrapper);
        if (ascInfoPO1 != null) {
            throw new ServiceBizException("所选经销商公司下已经存在售后信息");
        }
        Long factoryCompanyId = companyMapper.findFactoryIdByAppId(loginInfoDto.getAppId());
        if (factoryCompanyId == null) {
            throw new ServiceBizException("您输入的AppID可能不存在");
        }
        //保存服务站组织信息
        OrgPO orgPO = new OrgPO();
        orgPO.setCompanyId(ascInfoPO.getCompanyId().longValue());
        orgPO.setAppId(loginInfoDto.getAppId());
        orgPO.setOrgName(ascInfoPO.getAscName());
        orgPO.setOrgShortName(ascInfoPO.getAscName());
        orgPO.setOrgCode(ascInfoPO.getAscCode() + "SE");
        orgPO.setOrgShortName(ascInfoPO.getAscName());
        orgPO.setOrgType(ManageDictCodeConstants.ORG_TYPE_ASC);
        orgPO.setDataType(ManageDictCodeConstants.DATA_TYPE_BY_OWNER);
        orgPO.setIsValid(10011001);
        orgPO.setRecordVersion(0);
        orgPO.setOrgId(Long.MAX_VALUE);
        orgMapper.insert(orgPO);
        //保存服务站信息
        LambdaQueryWrapper<CompanyPO> queryWrapper1 = new QueryWrapper<CompanyPO>().lambda();
        queryWrapper1.eq(CompanyPO::getAppId, loginInfoDto.getAppId());
        queryWrapper1.eq(CompanyPO::getCompanyCode, ascInfoPO.getAscCode());
        CompanyPO companyPO1 = companyMapper.selectOne(queryWrapper1);
        if (null != companyPO1) {
            ascInfoPO.setAscCode(ascInfoPO.getAscCode());
        } else {
            ascInfoPO.setAscCode(ascInfoPO.getAscCode());
        }
        ascInfoPO.setAppId(loginInfoDto.getAppId());
        ascInfoPO.setOrgId(orgPO.getOrgId().intValue());
        ascInfoPO.setOrgType(ManageDictCodeConstants.ORG_TYPE_ASC);
        ascInfoPO.setValidStatus(BaseDataDictConstants.IS_VALID);
        ascInfoPO.setFactoryCompanyId(factoryCompanyId.intValue());
        ascInfoMapper.insert(ascInfoPO);
        //保存服务站组织关系信息
        CompanyPO companyPO = companyMapper.selectById(ascInfoPO.getCompanyId());
        TrOrgPO trOrgPO = new TrOrgPO();
        trOrgPO.setOrgId(orgPO.getOrgId());
        trOrgPO.setParentOrgId(orgPO.getOrgId());
        trOrgPO.setParentOrgId(companyPO.getOrgId().longValue());
        trOrgMapper.insert(trOrgPO);

        TrOrgPO trOrgPO1 = new TrOrgPO();
        trOrgPO1.setOrgId(orgPO.getOrgId());
        trOrgPO1.setParentOrgId(orgPO.getOrgId());
        trOrgPO1.setParentOrgId(ascInfoPO.getIsColdArea().longValue());
        trOrgMapper.insert(trOrgPO1);
        return ascInfoPO.getOrgId();
    }

    @Override
    public Integer updateAfterSaleInfo(AscInfoPO ascInfoPO) {
        LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        LambdaQueryWrapper<AscInfoPO> ascInfoPOLambdaQueryWrapper = new QueryWrapper<AscInfoPO>().lambda();
        ascInfoPOLambdaQueryWrapper.eq(AscInfoPO::getAscId, ascInfoPO.getAscId());
        AscInfoPO asc = ascInfoMapper.selectOne(ascInfoPOLambdaQueryWrapper);

        asc.setAscShortName(ascInfoPO.getAscShortName());
        asc.setIsColdArea(ascInfoPO.getIsColdArea());
        asc.setProvince(ascInfoPO.getProvince());
        asc.setCity(ascInfoPO.getCity());
        asc.setCounty(ascInfoPO.getCounty());
        asc.setAddress(ascInfoPO.getAddress());
        asc.setBank(ascInfoPO.getBank());
        asc.setBankAccount(ascInfoPO.getBankAccount());
        asc.setDutyNumber(ascInfoPO.getDutyNumber());
        asc.setStatus(ascInfoPO.getStatus());
        asc.setAscId(ascInfoPO.getAscId());
        asc.setAppId(loginInfoDto.getAppId());
        ascInfoMapper.updateById(asc);
        this.updateByOrg(asc,loginInfoDto.getCompanyId());


        return ascInfoPO.getAscId();
    }

    /**
     * 睿蓝新增售后信息
     * @param ascInfoDTO
     * @return
     */
    @Override
    public Integer insertAscInfo(AscInfoDTO ascInfoDTO){
        AscInfoPO ascInfoPO = ascInfoMapper.selectOne(new LambdaQueryWrapper<AscInfoPO>()
                .eq(AscInfoPO::getCompanyId, ascInfoDTO.getCompanyId())
                .eq(AscInfoPO::getAscCode, ascInfoDTO.getAscCode()));
        LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        Long factoryCompanyId = companyMapper.findFactoryIdByAppId(loginInfoDto.getAppId());
        if (factoryCompanyId == null) {
            throw new ServiceBizException("您输入的AppID可能不存在");
        }

        //保存服务站组织信息
        OrgPO orgPO = new OrgPO();
        orgPO.setCompanyId(ascInfoPO.getCompanyId().longValue());
        orgPO.setAppId(loginInfoDto.getAppId());
        orgPO.setOrgName(ascInfoPO.getAscName());
        orgPO.setOrgShortName(ascInfoPO.getAscName());
        orgPO.setOrgCode(ascInfoPO.getAscCode() + "SE");
        orgPO.setOrgShortName(ascInfoPO.getAscName());
        orgPO.setOrgType(ManageDictCodeConstants.ORG_TYPE_ASC);
        orgPO.setDataType(ManageDictCodeConstants.DATA_TYPE_BY_OWNER);
        orgPO.setIsValid(10011001);
        orgPO.setRecordVersion(0);
        orgPO.setOrgId(Long.MAX_VALUE);
        orgMapper.insert(orgPO);

        //保存服务站信息
        ascInfoPO.setAppId(loginInfoDto.getAppId());
        ascInfoPO.setOrgId(orgPO.getOrgId().intValue());
        ascInfoPO.setOrgType(ManageDictCodeConstants.ORG_TYPE_ASC);
        ascInfoPO.setValidStatus(BaseDataDictConstants.IS_VALID);
        ascInfoPO.setFactoryCompanyId(factoryCompanyId.intValue());
        ascInfoMapper.insert(ascInfoPO);

        //保存服务站组织关系信息
        CompanyPO companyPO = companyMapper.selectById(ascInfoPO.getCompanyId());
        TrOrgPO trOrgPO = new TrOrgPO();
        trOrgPO.setOrgId(orgPO.getOrgId());
        trOrgPO.setParentOrgId(companyPO.getOrgId().longValue());
        trOrgMapper.insert(trOrgPO);

        return ascInfoPO.getOrgId();
    }

    /**
     * 更新tm_org
     */
    public void updateByOrg(AscInfoPO ascInfoPO,Long companyId) {
        OrgPO orgPO = orgMapper.selectById(ascInfoPO.getOrgId().longValue());
        if(orgPO!=null) {
            orgPO.setOrgName(ascInfoPO.getAscName());
            orgPO.setOrgShortName(ascInfoPO.getAscName());
            orgMapper.updateById(orgPO);

            if (!StringUtils.isNullOrEmpty(ascInfoPO.getIsColdArea())) {
                //删除原来的上级组织
                orgService.deleteById(ascInfoPO.getOrgId().longValue(), companyId);
                //新增组织关系
                TrOrgPO po = new TrOrgPO();
                po.setOrgId(orgPO.getOrgId());
                po.setParentOrgId(ascInfoPO.getIsColdArea().longValue());
                trOrgMapper.insert(po);
            }
        }
    }

}
